package com.hencoder.hencoderpracticedraw1.practice;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.hencoder.hencoderpracticedraw1.R;

public class Practice5DrawOvalView extends View {

    private int width;
    private int height;
    private float ovalWidth;
    private float ovalHeigth;

    public Practice5DrawOvalView(Context context) {
        super(context);
    }

    public Practice5DrawOvalView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Practice5DrawOvalView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.Practice5DrawOvalView);
        ovalWidth = array.getDimension(R.styleable.Practice5DrawOvalView_ovalWidth, 100);
        ovalHeigth = array.getDimension(R.styleable.Practice5DrawOvalView_ovalHeigth, 100);
        array.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        练习内容：使用 canvas.drawOval() 方法画椭圆
        Paint paint = new Paint();
        paint.setColor(Color.RED);
//        canvas.drawOval(width/2-ovalWidth/2,height/2-ovalHeigth/2,width/2+ovalWidth/2,height/2+ovalHeigth/2,paint);
        RectF oval = new RectF(width/2-ovalWidth/2,height/2-ovalHeigth/2,width/2+ovalWidth/2,height/2+ovalHeigth/2);
        canvas.drawOval(oval,paint);
    }
}
